package com.gxa.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Query;
import com.gxa.common.utils.RandomString;
import com.gxa.modules.sys.dto.CdkeyAddDto;
import com.gxa.modules.sys.dto.CouponDto;
import com.gxa.modules.sys.dto.PurchaseAddDto;
import com.gxa.modules.sys.entity.Area;
import com.gxa.modules.sys.entity.Cdkey;
import com.gxa.modules.sys.mapper.AreaMapper;
import com.gxa.modules.sys.mapper.CodeMapper;
import com.gxa.modules.sys.mapper.CouponMapper;
import com.gxa.modules.sys.mapper.PurchaseMapper;
import com.gxa.modules.sys.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class CodeServiceImpl extends ServiceImpl<CodeMapper, Cdkey> implements CodeService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public PageUtils queryList(Map<String,Object> params) {
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        String areaId = (String) params.get("areaId");
        String tName = (String) params.get("tName");
        String state = (String) params.get("state");
        String code = (String) params.get("code");
        String platform = (String) params.get("platform");
        IPage<Cdkey> page = this.page(new Query<Cdkey>().getPage(params),
                new QueryWrapper<Cdkey>().eq(StringUtils.isNotEmpty(startTime),"start_time",startTime)
                        .eq(StringUtils.isNotEmpty(endTime),"end_time",endTime)
                        .eq(StringUtils.isNotEmpty(areaId),"area_id",areaId)
                        .eq(StringUtils.isNotEmpty(tName),"t_name",tName)
                        .eq(StringUtils.isNotEmpty(state),"state",state)
                        .eq(StringUtils.isNotEmpty(code),"code",code)
                        .eq(StringUtils.isNotEmpty(platform),"platform",platform)
        );
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }

    @Override
    public void addCode(CdkeyAddDto cdkeyAddDto) {

        for (int i = 0;i < cdkeyAddDto.getNumber();i++){
            //生成长度是20的随机字符串
            RandomString gen = new RandomString(20, ThreadLocalRandom.current());
            Date date = new Date(System.currentTimeMillis());
            cdkeyAddDto.setCode(gen.getString());
            cdkeyAddDto.setCreateTime(date);
            cdkeyAddDto.setState(1);

            this.baseMapper.addCode(cdkeyAddDto);
        }


    }


    @Transactional
    @Override
    public String exchange(CdkeyAddDto cdkeyAddDto) {
        Cdkey cdkey = this.baseMapper.selectOne(new QueryWrapper<Cdkey>().eq("code", cdkeyAddDto.getCode()));

        System.out.println("查询到的是-------"+cdkey);

        long endTime = cdkey.getEndTime().getTime();  //到期时间
        Date date1 = new Date(System.currentTimeMillis()); //当前时间
        long time = date1.getTime();

        if (cdkey.getState() != 1){
            return "兑换码失效";
        }
//        else if (time > endTime){ //判断当前时间是否大于到期时间 如果大于到期时间 表示过期， 返回告知 再次重置状态
//            Cdkey cd = new Cdkey();
//            cd.setId(cdkey.getId());
//            cd.setState(2);
//            this.baseMapper.updateById(cd);
//          return "超过使用日期";
//        }

        CouponDto couponDto = new CouponDto();
        couponDto.setAreaId(cdkey.getAreaId());
        couponDto.setName(cdkey.getName());

        Integer id = this.couponMapper.queryId(couponDto);//根据cdkey类型 查询对应套餐的id

        System.out.println("套餐id是++++++++"+id);

        //设置套餐dto
        PurchaseAddDto purchaseAddDto = new PurchaseAddDto();
        purchaseAddDto.setUserId(cdkeyAddDto.getUserId()); //设置用户id
        purchaseAddDto.setCouponId(id);                    //设置套餐id
        purchaseAddDto.setChannel(2);                       //设置渠道
        Date date = new Date(System.currentTimeMillis());  //获取当前时间
        purchaseAddDto.setBuyTime(date);                      //设置当前时间

        this.purchaseMapper.addpurchase(purchaseAddDto);  //添加卡卷

        Cdkey cdkey1 = new Cdkey();
        cdkey1.setId(cdkey.getId());
        cdkey1.setState(3);

        this.baseMapper.updateById(cdkey1);  //添加成功后设置cdkey 状态为已使用

        return "兑换成功";
    }

    @Override
    public void updateState(Date date) {

        this.baseMapper.updateStateArea(date);
    }

    @Override
    public List<Area> queryArea() {

        List<Area> areas = this.areaMapper.queryArea();

        return areas;
    }

}
